<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<%@ include file="/WEB-INF/views/common.jsp"%>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<script type="text/javascript">
	$(function(){
		var teacherDatagrid = $("#teacherDatagrid");
		var teacherDialog = $("#teacherDialog");
		var addForm = $("#addForm");
		var cmdObject = {
				addTeacher : function() {
					teacherDialog.dialog('open').dialog('setTitle', '新增管理员');
					addForm.form('clear');
					$("#error").text("");
				},
				saveTeacher : function() {
					var pass1 = $("#password").val();
					var pass2 = $("#againPass").val();
					if(pass1 != pass2) {
						$("#error").text("两次输入的密码不一致！");
						return ;
					}
					addForm.form('submit',{
						url : "/teacher/save",
						onSubmit : function() {
							return $(this).form('validate');
						},
						success : function(data) {
							var data = JSON.parse(data);
							if(data.code == 0) {
								//重新刷新departmentDatagrid
								teacherDatagrid.datagrid('reload');
								teacherDialog.dialog('close');	
							} else {
								$.messager.alert('提示', data.msg, 'error');
							}
						}
					})
				},
				updateTeacher : function() {
					row = teacherDatagrid.datagrid('getSelected');
					if (!row) {
						$.messager.alert('消息提示', '修改前请选中一行!', 'info');
						return;
					}
					addForm.form('clear');
					teacherDialog.dialog('open').dialog('setTitle', '修改管理员');
					addForm.form('load', row);
				},
				deleteTeacher : function() {
					var row = teacherDatagrid.datagrid('getSelected');
					if (!row) {
						$.messager.alert('提示', '删除前请选中一行', 'info');
						return;
					}
					$.messager.confirm('提示', '确定要删除这条记录？', function(r) {
						if (r) {
							$.get("/teacher/delete?id=" + row.id, function(data) {
								if (data.code == 0) {
									//重新刷新Datagrid
									teacherDatagrid.datagrid('reload');
								} else {
									$.messager.alert('提示', data.msg, 'error');
								}
							});
						}
					})
				},
				cancelTeacher : function() {
					teacherDialog.dialog('close');
				}
				
			}
			
			$("a[data-cmd]").on('click', function() {
				var cmd = $(this).data("cmd");
				if (cmd && !$(this).hasClass('l-btn-disabled')) {
					cmdObject[cmd]();
				}
			});
	});
	
	function timeFormat(value, row, index) {
		if(value == null || value == '') {
			return null;
		}
		var date = new Date(value);
		return date.format("yyyy-MM-dd hh:mm:ss");
	}
	
	Date.prototype.format = function(format) {
		var o = {
			"M+" : this.getMonth() + 1, //month 
			"d+" : this.getDate(), //day 
			"h+" : this.getHours(), //hour 
			"m+" : this.getMinutes(), //minute 
			"s+" : this.getSeconds(), //second 
			"q+" : Math.floor((this.getMonth() + 3) / 3), //quarter 
			"S" : this.getMilliseconds()
		//millisecond 
		}
		if (/(y+)/.test(format)) {
			format = format.replace(RegExp.$1, (this.getFullYear() + "")
					.substr(4 - RegExp.$1.length));
		}
		for ( var k in o) {
			if (new RegExp("(" + k + ")").test(format)) {
				format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k]
						: ("00" + o[k]).substr(("" + o[k]).length));
			}
		}
		return format;
	}
	
	function passFormat(value, row, index) {
		return '******';
	}
</script>
<body>
	<table id="teacherDatagrid" class="easyui-datagrid"
		toolbar="#teacherTooBar" url="/teacher/list" pagination="true"
		fit="true" fitColumns="true" singleSelect="true" striped="true">
		<thead>
			<tr>
				<th field="id" align="center" width="20">编号</th>
				<th field="username" align="center" width="40">用户名</th>
				<th field="password" align="center" width="40" formatter="passFormat">密码</th>
				<th field="lastLoginTime" align="center" width="40" formatter="timeFormat">上次登录时间</th>
				<th field="lastLoginIp" align="center" width="40">上次登录IP</th>
				<th field="createTime" align="center" width="40" formatter="timeFormat">创建时间</th>
			</tr>
		</thead>
	</table>
	<div id="teacherTooBar">
		<a id="add" href="javascript:void(0)" data-cmd="addTeacher"
			class="easyui-linkbutton" iconCls="icon-add">新增管理员</a> <a id="update"
			href="javascript:void(0)" class="easyui-linkbutton"
			iconCls="icon-edit" data-cmd="updateTeacher">修改管理员</a> <a id="delete"
			href="javascript:void(0)" class="easyui-linkbutton"
			iconCls="icon-remove" data-cmd="deleteTeacher">删除管理员 </a>
	</div>
	<div id="teacherDialog" class="easyui-dialog"
		style="width: 300px; height: 200px; padding: 10px 20px" closed="true"
		buttons="#DialogButtons">
		<form id="addForm" class="fm" method="post" novalidate>
			<input type="hidden" name="id">
			<table>
				<tr>
					<div class="fitem">
						<td><label>用户名:</label></td>
						<td><input name="username" class="easyui-validatebox"
							required="true"></td>
					</div>
				</tr>
				<tr>
					<div class="fitem">
						<td><label>密码:</label></td>
						<td><input type="password" name="password" id="password" class="easyui-validatebox"
							required="true"></td>
					</div>
				</tr>
				<tr>
					<div class="fitem">
						<td><label>再次输入密码:</label></td>
						<td><input type="password" id="againPass" class="easyui-validatebox"
							required="true"></td>
					</div>
				</tr>
			</table>
			<span id="error"
				style="margin-left: 80px; color: red; font-size: 14px; float: left"></span>
		</form>
	</div>
	<div id="DialogButtons">
		<a id="save" data-cmd="saveTeacher" href="javascript:void(0)"
			class="easyui-linkbutton" iconCls="icon-ok">保存</a> <a id="cancel"
			data-cmd="cancelTeacher" href="javascript:void(0)"
			class="easyui-linkbutton" iconCls="icon-cancel">取消</a>
	</div>
</body>
</html>